package com.chuantou.yuba.ui.widget;

import java.util.HashMap;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.RequestManager;
import com.chuantou.yuba.api.UriHelper;
import com.chuantou.yuba.api.request.GsonRequest;
import com.chuantou.yuba.api.to.BaseTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.ChatActivity;
import com.chuantou.yuba.ui.LoginActivity;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.ActivityHelper;
import com.chuantou.yuba.util.ChatUtils;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.UiUtil;
import com.easemob.chat.EMChatManager;

public class ChatMorePopup extends PopupWindow implements View.OnClickListener {

	private View mContentView;
	private TextView mBlackListView;
	private TextView mProfileView;
	private TextView mReportView;
	private ChatActivity mActivity;
	private long mUid;

	public ChatMorePopup(ChatActivity activity, long uid) {
		super(activity);
		mActivity = activity;
		mUid = uid;
		mContentView = View.inflate(activity, R.layout.chat_more_popup, null);
		AccountManager accountManager = AccountManager.getInstance(activity.getApplicationContext());
		UserTO loginUser = accountManager.getUserTO();
		mBlackListView = (TextView) mContentView.findViewById(R.id.black_list);
		mProfileView = (TextView) mContentView.findViewById(R.id.profile);
		mReportView = (TextView) mContentView.findViewById(R.id.report);
		if (loginUser == null || String.valueOf(loginUser.getUid()).equals(uid)) {
			mBlackListView.setVisibility(View.GONE);
			mProfileView.setVisibility(View.GONE);
			mReportView.setVisibility(View.GONE);
		}
		mBlackListView.setText( R.string.add_black_list);
		mBlackListView.setOnClickListener(this);
		mProfileView.setOnClickListener(this);
		mContentView.setOnClickListener(this);
		mReportView.setOnClickListener(this);
		mContentView.findViewById(R.id.cancel).setOnClickListener(this);
		setContentView(mContentView);
		setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
		setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
		setFocusable(true);
		setAnimationStyle(R.style.popup_style);
		ColorDrawable drawable = new ColorDrawable(activity.getResources().getColor(R.color.translucence));
		setBackgroundDrawable(drawable);
	}

	public void dismissPopup() {
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.to_bottom);
		mContentView.clearAnimation();
		animation.setFillAfter(true);
		animation.setAnimationListener(new DismissAnimationListener() {

			@Override
			public void onAnimationEnd(Animation animation) {
				super.onAnimationEnd(animation);
				new Handler().post(new Runnable() {

					@Override
					public void run() {
						ChatMorePopup.super.dismiss();
					}
				});
			}
		});
		mContentView.setAnimation(animation);
	}

	@Override
	public void dismiss() {
		dismissPopup();
	}

	@Override
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		super.showAsDropDown(anchor, xoff, yoff);
		startShowAnimation();
	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		startShowAnimation();
	}

	private void startShowAnimation() {
		mContentView.clearAnimation();
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.from_bottom);
		animation.setFillAfter(true);
		mContentView.setAnimation(animation);
	}

	class DismissAnimationListener implements Animation.AnimationListener {

		private int position = -1;

		public DismissAnimationListener() {
		}

		public DismissAnimationListener(int position) {
			this.position = position;
		}

		public int getPosition() {
			return position;
		}

		@Override
		public void onAnimationStart(Animation animation) {

		}

		@Override
		public void onAnimationEnd(Animation animation) {

		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}

	}

	@Override
	public void onClick(View v) {
		if (UiUtil.isFastDoubleClick()) {
			return;
		}
		dismiss();
		if (v == mBlackListView) {
			onBlackListClick();
		} else if (v == mProfileView) {
			ActivityHelper.toProfileActivity(mActivity, mUid);
		} else if (v == mReportView) {
			ActivityHelper.toReportActivity(mActivity, mUid);
		} else if (v == mContentView || v.getId() == R.id.cancel) {
			dismiss();
		}
	}

	public void onBlackListClick() {
		AccountManager accountManager = AccountManager.getInstance(mActivity);
		if (!accountManager.isLogined()) {
			UiUtil.showNoLoginDialog(mActivity, -1, LoginActivity.ACTION_RETURN);
			return;
		} else {
			UserTO user = accountManager.getUserTO();
			if (user.isExpired()) {
				Toast.makeText(mActivity, R.string.message_token_expired, Toast.LENGTH_SHORT).show();
				LoginActivity.start(mActivity, -1, LoginActivity.ACTION_RETURN);
			} else {
				String url = UriHelper.getAddBlackListUrl();
				HashMap<String, String> params = new HashMap<String, String>();
				params.put(UriHelper.PARAMS_TOKEN, user.getToken());
				params.put(UriHelper.PARAMS_MATCH_ID, String.valueOf(mUid));
				params.put(UriHelper.PARAMS_UID, String.valueOf(user.getUid()));
				GsonRequest<BaseTO> request = new GsonRequest<BaseTO>(Method.POST, url, new Listener<BaseTO>() {

					@Override
					public void onResponse(BaseTO response) {
						if (!mActivity.hasDestroyed()) {
							if (response != null) {
								if (response.isSuccess()) {
									new Thread(new Runnable() {
										public void run() {
											ChatUtils.sysBlackList();
										}
									}).start();
									mActivity.deleteCurrentConversation();
									Toast.makeText(mActivity, R.string.operate_success, Toast.LENGTH_SHORT).show();
									mActivity.sendBroadcast(new Intent(Constants.INTENT_ACITON_BLACK_LIST_CHANGED));
								} else {
									if (response.getStatus() == BaseTO.STATUS_TOKEN_ERROR || response.getStatus() == BaseTO.STATUS_TOKEN_EXPIRED) {
										Toast.makeText(mActivity, R.string.message_token_expired, Toast.LENGTH_SHORT).show();
										LoginActivity.start(mActivity, -1, LoginActivity.ACTION_RETURN);
									} else {
										String msg = response.getMsg();
										msg = TextUtils.isEmpty(msg) ? mActivity.getString(R.string.operate_failed) : msg;
										Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
									}
								}
							} else {
								Toast.makeText(mActivity, R.string.operate_failed, Toast.LENGTH_SHORT).show();
							}
						}else{
							EMChatManager.getInstance().deleteConversation(ChatUtils.getHXAccount(mUid));
						}
					}
				}, new ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						if (!mActivity.hasDestroyed()) {
							Toast.makeText(mActivity, R.string.request_error, Toast.LENGTH_SHORT).show();
						}
					}
				}, params, BaseTO.class);
				RequestManager.startRequest(mActivity, request);
			}
		}
	}
}